package com.qby.framework.swagger2;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: Swagger2配置类
 * @author: qiu bo yang
 * @create: 2020-05-12 11:54
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket accessToken() {//核心配置
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qby.project.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(globalOperation());       // 主要关注点----每个接口调用都填写token
    }

    /**
     * http://localhost:8080/koiWorld/swagger-ui.html#
     *
     * @return
     */
    private ApiInfo apiInfo() {//定义项目说明
        return new ApiInfoBuilder()
                .title("锦鲤世界")
                .description("app开发")
                .termsOfServiceUrl("http://jinlishijie.com/koiWorld/")
                .contact(new Contact("邱博洋", "http://jinlishijie.com/koiWorld/", "1057731576@qq.com")
                ).version("1.0").build();//版本号
    }

    private List<Parameter> globalOperation() {
        //添加head参数配置start
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameterList = new ArrayList<>();
        //第一个token为传参的key，第二个token为swagger页面显示的值
        parameterBuilder.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        parameterList.add(parameterBuilder.build());
        return parameterList;
    }
}
